package com.heima.wemedia.interceptor;

import com.heima.model.wemedia.pojo.WmUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 */
@Component
public class WmTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从请求头中获取用户id，并存入ThreadLocal共享
        String userId = request.getHeader("userId");
        if (userId != null) {
            WmUser user = new WmUser();
            user.setId(Integer.parseInt(userId));
            WmThreadLocalUtil.setUser(user);
        }
        return true;
    }
}
